/**
 * 转账管理
 */
<template>
  <div>
    <!-- 面包屑导航 -->
    <el-breadcrumb separator-class="el-icon-arrow-right" class="margin-t10">
      <el-breadcrumb-item :to="{ path: '/' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>转账管理</el-breadcrumb-item>
    </el-breadcrumb>

    <div class="boxBlock">
      <div class="serachTop">
        <!-- 搜索筛选 -->
          <el-form :inline="true" :model="formInline" class="user-search">
            <el-form-item  >
              <el-input
                size="small"
                v-model="formInline.orderNo"
                placeholder="输入订单编号"
                type="number"
                clearable
              ></el-input>
            </el-form-item>
            <el-form-item label="">
              <el-select
                size="small"
                v-model="formInline.type"
                placeholder="请选择转账类型"
                clearable
              >
                <el-option
                  v-for="type in typeData"
                  :label="type.key"
                  :value="type.value"
                  :key="type.value"
                ></el-option>
              </el-select>
            </el-form-item>
            <el-form-item label="">
              <el-select
                size="small"
                v-model="formInline.status"
                placeholder="请选择审核状态"
                clearable
              >
                <el-option label="请选择审核状态" value=""></el-option>
                <el-option
                  v-for="type in statusData"
                  :label="type.key"
                  :value="type.value"
                  :key="type.value"
                ></el-option>
              </el-select>
            </el-form-item>

            <!-- <el-form-item prop="createdAtStart">
              <el-date-picker
                v-model="formInline.createdAtStart"
                type="date"
                placeholder="选择开始日期"

              >
              </el-date-picker>
            </el-form-item>
            <el-form-item label=""> — </el-form-item>
            <el-form-item prop="createdAtEnd">
              <el-date-picker
                v-model="formInline.createdAtEnd"
                type="date"
                placeholder="选择结束日期"
              >
              </el-date-picker>
            </el-form-item> -->
            <el-form-item>
              <el-button
                size="small"
                type="primary"
                icon="el-icon-search"
                @click="search"
                >搜索</el-button
              >
              <el-button
                size="small"
                type="primary"
                icon="el-icon-plus"
                @click="handleEdit()"
                >转账</el-button
              >
            </el-form-item>
          </el-form>

          <el-button size="small" icon="el-icon-download"  type="primary" @click="exportEx()">
            导出
          </el-button>
      </div>
 
    
    <!--列表-->
    <div class="ra_box"> 
      <el-table
        size="small"
        :data="listData"
        highlight-current-row
        v-loading="loading"
        element-loading-text="拼命加载中"
        style="width: 100%"
        v-if="listData"
      >
        <el-table-column prop="orderNo" label="订单编号" show-overflow-tooltip>
        </el-table-column>
        <el-table-column prop="userName" label="支付用户" show-overflow-tooltip>
        </el-table-column>
        <el-table-column prop="amount" label="转账金额" show-overflow-tooltip>
          <template slot-scope="scope">
            <div>{{ b(scope.row.amount) }}</div>
          </template>
        </el-table-column>
        <el-table-column
          prop="toUserName"
          label="收款用户"
          show-overflow-tooltip
        >
        </el-table-column>
        <el-table-column
          prop="createTime"
          label="转账时间"
          show-overflow-tooltip
        >
          <template slot-scope="scope">
            {{ timestampToTime(scope.row.createTime) }}
          </template>
        </el-table-column>
        <el-table-column prop="status" label="审核状态" width="100">
          <template slot-scope="scope">
            <div>{{ statusData[scope.row.status].key }}</div>
          </template>
        </el-table-column>
        <el-table-column
          align="center"
          label="操作"
          width="150"
          show-overflow-tooltip
        >
          <template slot-scope="scope">
            <el-button size="mini" plain @click="handleEdit('editForm', scope.row)">
              查看
            </el-button>
          </template>
        </el-table-column>
      </el-table>
    </div>
    <!-- 分页组件 -->
    <Pagination
      v-bind:child-msg="pageparm"
      @callFather="callFather"
    ></Pagination>

  </div> 
    <!-- 查看界面 -->
    <el-dialog
      :close-on-click-modal="false"
      title="查看"
      :visible.sync="editFormVisible"
      width="30%"
      @close="closeDialog('editForm')"
    >
      <el-form :model="editForm" ref="editForm">
        <el-form-item label="订单编号" prop="orderNo">
          {{ editForm.orderNo }}
        </el-form-item>
        <el-form-item label="支付用户" prop="userName">
          {{ editForm.userName }}
        </el-form-item>
        <el-form-item label="金额(USD)" prop="transferAmount">
          {{ editForm.amount }}
        </el-form-item>
        <el-form-item label="收款用户" prop="toUserName">
          {{ editForm.toUserName }}
        </el-form-item>
        <el-form-item label="转账时间" prop="createTime">
          {{ timestampToTime(editForm.createTime) }}
        </el-form-item>
        <el-form-item label="备注" prop="remark">
          {{ editForm.remark }}
        </el-form-item>
        <el-form-item label="审核状态" prop="status" v-if="editForm.status">
          {{ statusData[editForm.status].key }}
        </el-form-item>
        <el-form-item
          label="审核时间"
          prop="updateTime"
          v-if="editForm.updateTime"
        >
          {{ timestampToTime(editForm.updateTime) }}
        </el-form-item>

        <i class="el-icon-warning-outline font12">
          转账需要填写接收账户的收款码，审核需要1~2天，请耐心等待
        </i>
      </el-form>
    </el-dialog>
    <!-- 转账界面  -->
    <el-dialog
      :close-on-click-modal="false"
      title="转账"
      :visible.sync="addFormVisible"
      width="600px"
      @close="closeDialog('addForm')"
    >
      <el-form :model="addForm" label-width="100px" :rules="rules" ref="addForm">
        <el-form-item label=" " prop=" ">
          <el-tag type="danger">
          <i class="el-icon-warning-outline font12">
            转账需要填写接收账户的收款码，审核需要1~2天，请耐心等待
          </i>
        </el-tag>
        </el-form-item>  
        <el-form-item label="金额(USD)" prop="transferAmount">
          <el-input
            size="small"
            v-model="addForm.transferAmount"
            auto-complete="off"
            placeholder="请输入金额(USD)"
            style="width: 400px;"
          ></el-input>
        </el-form-item>
        <el-form-item label="收款码" prop="receivingCode">
          <el-input
            size="small"
            v-model="addForm.receivingCode"
            auto-complete="off"
            placeholder="请输入收款码"
            style="width: 400px;"
          ></el-input>
        </el-form-item>
        <el-form-item label="备注" prop="remark">
          <el-input
            size="small"
            v-model.trim="addForm.remark"
            auto-complete="off"
            type="textarea"
            :rows="5"
            placeholder="请输入备注"
            style="width: 400px;"
          ></el-input>
        </el-form-item> 
       
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button size="small" @click="closeDialog('addForm')">取消</el-button>
        <el-button
          size="small"
          type="primary"
          :loading="loading"
          class="title"
          @click="submitForm('addForm')"
          >提交</el-button
        >
      </div>
    </el-dialog>
    <codeChoose ref="codeRef" :fromType="verType" @setParam="getParam" ></codeChoose>
  </div>
</template>

<script>
import { mapState, mapGetters } from "vuex";
import codeChoose from '@/components/codeChoose.vue'
import {
  setStoreS,
  getStoreS,
  removeStoreS,
  insertCharacter,
  timestampToTime,
  spStr,
  
} from "../../utils/util";
import { baseURL, service, format, getUrl,reqJson } from "../../api/axiosFun";
import Pagination from "../../components/Pagination";
export default {
  data() {
    return {
      loading: false,
      editFormVisible: false,
      addFormVisible: false,
      b: format,
      timestampToTime: timestampToTime,
      statusData: [
        { key: "待审核", value: 0 },
        { key: "通过", value: 1 },
        { key: "驳回", value: 2 },
      ],
      typeData: [
        { key: "转账", value: 0 },
        { key: "收款", value: 1 },
      ],
      editForm: {},
      addForm: {
        receivingCode: "",
        transferAmount: "",
        remark: "",
      },
      // rules表单验证
      rules: {
        transferAmount: [
          { required: true, message: "请输入转账金额", trigger: "blur" },
          {
            pattern: /^\d+(\.\d{1,2})?$/,
            message: "请输入正整数或者2位数小数",
            trigger: "blur",
          },
        ],
        receivingCode: [
          { required: true, message: "请输入收款码", trigger: "blur" },
        ],
      },
      formInline: {
        pageNum: 1,
        pageSize: 10,
        status: "",
        createdAtStart: "",
        createdAtEnd: "",
        type: 0,
      },
      listData: [], //用户数据
      // 分页参数
      pageparm: {
        currentPage: 1,
        pageSize: 10,
        total: 10,
      },
      headers:'',
      verArry:[],
      verType:'',
      curItem:'', 
      userInfo: null,
      fd:''
    };
  },
  // 注册组件
  components: {
    Pagination,
    codeChoose
  }, 
  computed: {
    ...mapGetters(["getUser"]),
  },
  watch: {
    getUser(newVal) {
      this.userInfo = JSON.parse(newVal);
    },
  },

  created() {
    this.getdata(this.formInline);
    this.getInfo();
  },

  /**
   * 里面的方法只有被调用才会执行
   */
  methods: {
    exportEx() {
      let url = getUrl(this.formInline);
      service
        .post("/api/photon/exportTransferRecordList" + url, {})
        .then((res) => {
          this.loading = false;
          console.log(res);
          if (res.code == 0) {
            window.open(
              baseURL + "/common/download?fileName=" + res.msg,
              "_blank"
            );
          } else {
            this.$message({
              type: "info",
              message: res.msg,
            });
          }
        })
        .catch((err) => {
          this.loading = false;
          this.$message.error(err);
        });
    },
    getdata(parameter) {
      let url = getUrl(parameter);
      service
        .get("/api/photon/getTransferRecordList" + url)
        .then((res) => {
          this.loading = false;
          if (res.code == 0) {
            this.listData = res.rows;
            this.pageparm.currentPage = this.formInline.pageNum;
            this.pageparm.pageSize = this.formInline.pageSize;
            this.pageparm.total = res.total;
          } else {
            this.$message({
              type: "info",
              message: spStr(res.msg, "PhotonPay", ""),
            });
          }
        })
        .catch((err) => {
          this.$message.error(spStr(err, "PhotonPay", ""));
        });
    },
    // 分页插件事件
    callFather(parm) {
      this.formInline.pageNum = parm.currentPage;
      this.formInline.pageSize = parm.pageSize;
      this.getdata(this.formInline);
    },
    // 搜索事件
    search() {
      this.getdata(this.formInline);
    },
    //显示编辑界面
    handleEdit(name, row) {
      if (name == "editForm") {
        this.editFormVisible = true;
        this.editForm = row;
      } else {
        this.addFormVisible = true;
      }
    },

    getParam(val,type){  
      this.headers = val ;
      this.editSaveAuth() 
    }, 


    editSaveAuth(){
      let url = getUrl(this.addForm);
      reqJson('post',"/api/photon/transfer" + url,{},{
        headers:{
          ...this.headers
        }
      }).then((res) => {
        if (res.code == 0) {
            this.$message.success("提交成功，请等待审核...");
            this.closeDialog();
            this.getdata(this.formInline);
          } else {
            this.$message({
              type: "info",
              message: spStr(res.msg, "PhotonPay", ""),
            });
          }
        })
        .catch((err) => {
          this.$message.error(spStr(err, "PhotonPay", ""));
        });

    },
    getInfo() {
      service
        .get("/api/photon/getUserInfo", {})
        .then((res) => {
          this.loading = false;
          if (res.code == 0) {
            this.userInfo = res.data;
            this.$store.commit("user", JSON.stringify(res.data));
          } else {
            this.$message({
              type: "info",
              message: spStr(res.msg, "PhotonPay", ""),
            });
          }
        })
        .catch((err) => {
          this.$message.error(err);
        });
    },
    // 编辑、增加页面保存方法
    submitForm(editData) {
      this.$refs[editData].validate((valid) => {
        if (valid) { 
          // this.fd = fd;
          if(this.userInfo&&this.userInfo.verifyApi){
            this.verArry =  this.userInfo.verifyApi.split(',');
            if(this.verArry.includes('/api/photon/transfer')){
              this.$refs.codeRef.openDialog(); 
            }else{
              this.editSaveAuth();
            }
          }else{
            this.editSaveAuth();
          } 
        //   service
        //     .post("/api/photon/transfer" + url)
        //     .then((res) => {
        //       if (res.code == 0) {
        //         this.$message.success("提交成功，请等待审核...");
        //         this.closeDialog();
        //         this.getdata(this.formInline);
        //       } else {
        //         this.$message({
        //           type: "info",
        //           message: spStr(res.msg, "PhotonPay", ""),
        //         });
        //       }
        //     })
        //     .catch((err) => {
        //       this.$message.error(spStr(err, "PhotonPay", ""));
        //     });
        // } else {
        //   return false;
        // }
        }
      });
    },
    // 关闭编辑、增加弹出框
    closeDialog(name) {
      if (name == "editForm") {
        this.editFormVisible = false;
      } else {
        this.$refs.addForm.resetFields();
        this.addFormVisible = false;
      }
    },
  },
};
</script>

<style scoped>
.user-search {
  margin-top: 20px;
}
.userRole {
  width: 100%;
}
</style>

 
 